import { Component, Input, OnInit } from '@angular/core';
import { SFSchema } from '@delon/form';
import { ApiService } from '@core/services/api';
import { HttpClient } from '@angular/common/http';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { title } from 'process';
import { timeout } from 'rxjs/operators';

@Component({
  selector: 'upgrade-edit',
  templateUrl: './upgrade-edit.component.html',
  styles: []
})
export class UpgradeEditComponent implements OnInit {
  @Input()type: 0 | 1;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
    console.log(this.param);
    if(this.param.upgradeUrl) {
      this.fileList = [...[{
        id: '-1',
        status: 'done',
        url: this.param.upgradeUrl
      }]]
    }
  };
  param: any;
  uploadUrl = '/network/minIo/uploadFile';
  fileList: any[] = [

  ];
  showForm = false;
  schema: SFSchema = {
    properties: {
      upgradeWay: {
        type: 'number',
        title: '设备类型',
        enum: [
            {label: '门禁摄像机', value: 1},
            {label: '门禁刷卡', value: 2},
            {label: 'IPC', value: 3},
            {label: 'NVR', value: 4},
            {label: '微型门禁产品', value: 5},
            {label: '安卓门禁产品', value: 6},
            {label: '门铃', value: 7},
            {label: '4G开门控制器',value: 8},
            {label: 'linux人脸机',value: 12}
        ],
        ui: {
          widget: 'select'
        }
      },
      upgradeName: {
        type: 'string',
        title: '包名称'
      },
      upgradeFacCode: {
        type: 'string',
        title: '升级包厂商'
      },
      upgradeType: {
        type: 'number',
        title: '包类型',
        enum: [
          {label: '终端升级程序', value: 1},
          {label: '固件升级程序', value: 2},
          {label: 'MCU固件',value:3},
          {label: '读头固件',value:4},
          {label: 'IPC固件',value:5},
          {label: '单片机升级程序', value: 6}
        ],
        ui: {
          widget: 'select'
        }
      },
      upgradeFeature:{
        type:'string',
        title:'包描述'
      },
      upgradeVersion:{
        type:'string',
        title:'版本'
      },
      upgradeVersionStatus:{
        type:'number',
        title:'版本状态',
        enum:[
          {label:'启用',value:1},
          {label:'停用',value:0}
        ],
        ui:{
          widget:'select'
        }
      },
      upgradeSize:{
        type:'string',
        title:'文件大小'
      },


    },
    required: ['upgradeName','upgradeVersion','upgradeWay']
  }
  handle(event:any){
    if(event.type == 'success'){
      if(event.file.response.status != 200){
        this.msg.error(event.file.response.message)
      }else{
        this.msg.success(event.file.response.message)
      }
      
    }
  }
  formSubmit(data) {
    let fileUrl = '';
    if (this.fileList.length> 0 && (this.fileList[0].response.status == '200' || this.fileList[0].url)) {
      fileUrl = this.fileList[0].url ||  this.fileList[0].response.data;
    }
    if(fileUrl == ''){
      this.msg.error('未上传安装包');
      return;
    }
    const body: any = {
      upgradeWay:data.upgradeWay,
      upgradeName: data.upgradeName,
      upgradeSize: data.upgradeSize,
      upgradeFacCode: data.upgradeFacCode,
      upgradeType:data.upgradeType,
      upgradeUrl:fileUrl,
      upgradeFeature:data.upgradeFeature,
      upgradeVersion:data.upgradeVersion,
      upgradeVersionStatus:data.upgradeVersionStatus
    }
    const url = '/core/upgrade';
    const method = 'POST';
    this.http.request<any>(method, url,  { body}).subscribe(res => {
      console.log(res)
      if (res.status === '200' || res.status === '201') {
        this.msg.success('操作成功');
        this.modalRef.destroy('ok');
      } else {
        this.msg.error(res.message)
      }
    })

  }

  constructor(
    private api: ApiService,
    private http: HttpClient,
    private msg: NzMessageService,
    private modalRef: NzModalRef
  ) { }

  async ngOnInit() {
    this.showForm = true;
  }

}
